package com.itheima.zixi6.TCP2;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ServerReaderThread extends Thread{
    private Socket socket;
    public ServerReaderThread(Socket socket)
    {
        this.socket = socket;
    }

    @Override
    public void run() {
                try{//3.从socket通信管道中得到一个字节输入流
                    InputStream is = socket.getInputStream();
                    //4.把字节输入流转换成数据输入流
                    DataInputStream dis = new DataInputStream(is);
                    //5.读取客户端发送的数据
                    while (true) {
                        try {
                            String data = dis.readUTF();
                            System.out.println("服务器接收到的数据：" + data);
                            System.out.println(socket.getRemoteSocketAddress());
                        } catch (IOException e) {
                            System.out.println(socket.getRemoteSocketAddress()+"已经离线了");
                            socket.close();
                            dis.close();
                        }
                    }
                } catch (Exception e) {
                e.printStackTrace();
            }

    }
}
